package com.coder520.handlertest;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.PersistableBundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.widget.TextView;

/**
 * Created by huang on 2017/7/18.
 */

public class HandlerActivity extends Activity {
    private static final int MSG_UPDATE_COUNT = 1;
    private static final String TAG = HandlerActivity.class.getSimpleName();
    private TextView mTextCount;
    private int mCount = 0;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mTextCount = (TextView) findViewById(R.id.text_count);
        sendUpdateMessage();
        mHandler.postDelayed(mRunable, 1000);
    }

    private Runnable mRunable = new Runnable() {
        @Override
        public void run() {
            Log.d(TAG, "code520 ..... print something");
            //
        }
    };

    private void sendUpdateMessage() {
        Message message = new Message();
        message.what = MSG_UPDATE_COUNT;
        mHandler.sendMessageDelayed(message, 1000/*1s*/);
    }

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case MSG_UPDATE_COUNT:
                    mTextCount.setText(mCount + "");
                    mCount++;
                    sendUpdateMessage();
                    break;
                default:
                    break;
            }
        }
    };
}
